import {observable /*toJS*/} from 'mobx';
import {Tools,Bubble,Modal} from 'weaver-mobile';
import { RouteLayout,Upload,Form } from 'weaver-mobile-page';
import * as API from '../../api/home/reception';
import {getEquipment} from '../../util/common'
import {toJS} from 'mobx'
const weaAlert = Modal.alert;

const getLabel = Tools.getLabel;


export default class ReceptionStore {
    @observable loading = false;
    @observable hasRight = '';

    //当前设备
    equipment = {};

    @observable rememberGroup = [];

    @observable receptionList = [];

    /*     录入方式对应Page */
    @observable loading_page = false;

    //发票数据
    @observable invoiceInfo = [];

    @observable oldInvoiceLen = 0;
    @observable checkCode = '';
    @observable invoiceType = '';
    @observable invoiceNumber = '';
    @observable invoiceCode = '';
    @observable billingDate = '';   
    @observable form = {};
    @observable ocrInfo = {};
    @observable showSave = false;
    @observable showCheck = false;
    @observable showDelete = false;
    @observable invoiceType = '';

    @observable formAuto = {};
 
    //手动识别
    @observable formEnterByHand = {};
    @observable autoCheckRoute = new RouteLayout.Store({route:'/ocr',id:'autoCheck'});
    @observable checkJson = [];
    @observable loading_autoCheck = false;
    @observable invoiceResult = [];

    @observable currentInvoiceId = 0;//当前 发票ID；

    //影响识别
    @observable ocrRoute = new RouteLayout.Store({route:'/ocr',id:'invoiceDetaile'});
    @observable uploadStore = new Upload.Store({ route: '/home/reception/Ocr' });
    @observable ocrAutoCheckRoute = new RouteLayout.Store({route:'/ocr/invoiceDetaile',id:'autoCheck'});

    @observable toOcrInvoiceRoute = new RouteLayout.Store({route:'/rememberDetailPage/toOcrRoute',id:'toOcrInvoiceRoute'});
    //二维码
    @observable qRcodeParams = {};

    //图片base64
    @observable imgBase64 = '';


    @observable ebhAutoCheckRoute = new RouteLayout.Store({route:'/rememberDetailPage/toEnterByHand',id:'/ebhAutoCheckRoute'});

    /*             发票云             */
    @observable enabled = {};
    //连拍识别 数据
    @observable localIdArr = [];

    
    initData = (value={},iframeCallBack) =>{
        this.getEquipmentFn();
        this.getEnterInvoiceTap(value,iframeCallBack);
    }

    getEquipmentFn = () =>{
        this.equipment = getEquipment();
    }

    getEnterInvoiceTap = (value,iframeCallBack) =>{
        this.loading = true;
        API.getEnterInvoiceTap(value).then( res =>{
            const {status,invoiceCloud,data} = res;
            if(status == '1'){
                this.receptionList = data;
                if(invoiceCloud){
                    const { Ocr,QRcode,EnterByHand,weChatImport } = invoiceCloud;
                    this.enabled = {
                        scanCode: QRcode,
                        photo:Ocr,
                        importFromWX: weChatImport,
                        validate:true
                    }
                }
            }
            if(iframeCallBack){
                iframeCallBack();
            }else{
                this.loading = false;
            }
           
        })

    }

    
    //发票 识别  验票    ( 0：识别失败  1：识别成功 2：验票失败  3:验票成功 )
    enterInvoiceByOcr = (value={},callBack,imgSrc) =>{
        this.loading_page = true;
        // alert('enterInvoiceByOcr----------value');
        // alert(JSON.stringify(value));
        API.enterInvoiceByOcr(value).then( res =>{
            const {status,info,data} = res;
            // alert('res');
            // alert(JSON.stringify(res));

            if(value.operate == 'multiCheck'){//识别
                if(res.flag){//识别成功

                    data.map(items =>{
                        if(items.invoiceType == '10100' || items.invoiceType == '10101' || items.invoiceType == '10102' || items.invoiceType == '10103' || items.invoiceType == '10104'){
                            //alert(JSON.stringify(items));
                            if(items.needCheck){ //需要验票   
                                let _newImage = items.newImage;
                                items['newImage'] = undefined;
                                let _params = Object.assign(items,{
                                    operate:'invoiceCheck'
                                })  
           
                                this.enterInvoiceByOcr(_params,'',_newImage);
                            }else { //不需要验票
                                let isRepeat = this.getIsrepeat(items.invoiceId);
                                if(isRepeat){
                                    weaAlert(getLabel(131002,'发票代码')+items.invoiceId+getLabel(518094,'已经存在'),getLabel(518095,'已自动为你删除'),[{text: getLabel(33703, '确认')}])
                                }else{
                                    items['state'] = '1';
                                    this.invoiceInfo.push(items);
                                }
                               
                                // alert(' 识别成功,不需要验票invoiceType');
                                // alert(JSON.stringify(this.invoiceInfo));
                            }
                        }else { //不需要验票
                            let isRepeat = this.getIsrepeat(items.invoiceId);
                            if(isRepeat){
                                weaAlert(getLabel(131002,'发票代码')+items.invoiceId+getLabel(518094,'已经存在'),getLabel(518095,'已自动为你删除'),[{text: getLabel(33703, '确认')}])
                            }else{
                                items['state'] = '1';
                                this.invoiceInfo.push(items);
                            }
                            
                            // alert(' 识别成功,不需要验票');
                            // alert(JSON.stringify(this.invoiceInfo));
                        }
                        
                        if(res.exisdatatsInvoiceSb != '' && res.exisdatatsInvoiceSb){
                            Bubble.warning(res.exisdatatsInvoiceSb);
                        }

                        this.loading_page = false;
                    })
                }else {//识别失败
                    // alert('识别失败 this.invoiceInfo');
                    // alert(JSON.stringify(data));
                    let isRepeat = this.getIsrepeat(data[0].invoiceId);
                    if(isRepeat){
                        weaAlert(getLabel(131002,'发票代码')+data[0].invoiceId+getLabel(518094,'已经存在'),getLabel(518095,'已自动为你删除'),[{text: getLabel(33703, '确认')}])
                    }else{
                        data[0].state = '0';
                        this.invoiceInfo.push(data[0]);
                    }
                    this.loading_page = false;
                }
                    
            }else if(value.operate == 'invoiceCheck'){//验票
                if(!res.belongs){ //是否是本人发票
                    Bubble.warning(res.errorInfo);
                    this.loading_page = false;
                    return 
                }

                let isRepeat = this.getIsrepeat(res.invoiceId);
                if(isRepeat){
                    weaAlert(getLabel(131002,'发票代码')+res.data[0].invoiceId+getLabel(518094,'已经存在'),getLabel(518095,'已自动为你删除'),[{text: getLabel(33703, '确认')}])
                }else{
                    if(res.checkStatus == '1'){//验票陈宫
                        res['state'] = '3';
                    }else{//验票失败
                        res['state'] = '2';
                    }
                    res['newImage'] = imgSrc;
                    this.invoiceInfo.push(res);
                    // alert('验票');
                    // alert(JSON.stringify(this.invoiceInfo));
                }

                this.loading_page = false;
            }else if(value.operate == 'invoiceShow'){//发票显示
                this.showSave = res.showSave;
                this.showCheck = res.showCheck;
                this.showDelete = res.showDelete;
                this.invoiceType = res.type;
                if(res.showCheck){
                    this.formAuto = new Form.Store({route:value.currentRoute});
                    this.formAuto.initForm(res.conditions,value.currentRoute);
                    callBack && callBack(res.type);
                }else{
                    this.form = new Form.Store({route:value.currentRoute});
                    this.form.initForm(res.conditions,value.currentRoute);
                }
                this.loading_page = false;
            }else if(value.operate == 'deleteInvoice'){
                if(!res.belongs){
                    Bubble.warning(res.errorInfo);
                    this.loading_page = false;
                    return
                }

                callBack && callBack();
                this.loading_page = false;
            }else if(value.operate == 'authenticityCheck'){  //手动验票  单个验票
                if(!res.belongs){
                    Bubble.warning(res.errorInfo);
                    this.loading_page = false;
                    return
                }

                if(res.flag){
                    callBack && callBack(res.data)
                }else{
                    Bubble.error(res.msg);
                }
                this.loading_page = false;
            }else if(value.operate == 'saveInvoice'){ //保存
                if(!res.belongs){
                    Bubble.warning(res.errorInfo);
                    this.loading_page = false;
                    return
                }

                if(res.errorInfo){
                    Bubble.error(res.errorInfo);
                }else{
                    callBack && callBack(res.data)
                }
                this.loading_page = false;
            }

            
        })


    }

    //获取手动识别
    getHandSetPage = (value,callBack) =>{
        this.loading_page = true;
        API.getHandSetPage(value).then( res =>{
            const {status,fplx} = res;
            // alert(JSON.stringify(res));
            if(status == '1'){
                this.formEnterByHand = new Form.Store({route:value.crrentRoute});
                this.formEnterByHand.initForm(res.conditions,value.crrentRoute);
                fplx !== '' && callBack && callBack(fplx);
            }else{

            }
            this.loading_page = false;
        })
    }

    //手动识别  自动查验
    getInvoiceInfoByCheck = (value={},callBack)=>{
        this.loading_page = true;
        API.getInvoiceInfoByCheck(value).then( res =>{
            const {status,invoiceResult} = res;
            if(status == '1'){
                if(invoiceResult){
                    this.invoiceResult = invoiceResult;
                    this.checkJson = res.checkJson;
                    callBack && callBack();
                }else{
                    Bubble.error(res.checkJson.errMsg);
                    
                }
                
                
            }else{
                Bubble.error(res.errorInfo);
            }
            this.loading_page = false;
        })
    }

    //保存 自动查验
    saveInvoiceByCheck = (value={},callBack)=>{
        this.loading_autoCheck = true;
        API.saveInvoiceByCheck(value).then( res =>{
            const {status,invoiceId} = res;
            if(status == '1'){
                Bubble.success(getLabel(517140,'保存成功'));
                this.currentInvoiceId = invoiceId;
                callBack && callBack();
            }else{
                Bubble.error(res.errorInfo);
            }
            this.loading_autoCheck = false;
        })
    }
    
    setLoading = (value) =>{
        this.loading = value;
    }

    //更新 发票数据
    setInvoiceInfo = (value=[]) =>{
        this.invoiceInfo = value;
    }

    setOldInvoiceLen = (value) =>{
        this.oldInvoiceLen = value;
    }

    //保存ocr 数据
    setOcrInfo = (value={}) =>{
        this.ocrInfo = value;
    }

    //验证是否重复
    getIsrepeat = (id) =>{
        let bool = false;
        this.invoiceInfo.map(c =>{
            if(c.invoiceId == id){
                bool = true;
                return bool;
            }
        })
        return bool
    }

    //二维码识别结果
    setQRcodeParams = (value) =>{
        this.qRcodeParams = value;
    }

    //imgBase64
    setImgBase64 = (value) =>{
        this.imgBase64 = value;
    }

    setCurrentInvoiceId = (value) =>{
        this.currentInvoiceId = value;
    }

    /*发票云*/
    clearData = () =>{
        this.enabled = {};
    }

    setLocalIdArr = (value=[]) =>{
        this.localIdArr = value;
    }

    



    
}